package com.dhcc.core.framework.base.entity;

import java.util.List;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.util.CommonUtil;

/**
 * 树数据Entity类
 * 
 * @ClassName: TreeEntity
 * @Description: TODO
 * @author: cyf
 * @date: 2018年3月8日 下午4:21:20
 * @param <T>
 */
public abstract class TreeEntity<T extends Model<?>> extends DataEntity<T> {

    private static final long serialVersionUID = 1L;

    // 父id
    protected Long parentId;
    // 节点层次
    protected Integer levels;
    // 路径
    protected String path;
    // 排序
    protected Integer weight;

    @TableField(exist = false)
    protected List<T> children;

    @TableField(exist = false)
    protected T parentTree;

    public TreeEntity() {
        super();
    }

    public TreeEntity(Long id) {
        super(id);
    }

    /**
     * 根据请求的父级编号设置和层级
     */
    public void init() {
        if (CommonUtil.isEmpty(this.getParentId()) || this.getParentId() == 0) {
            this.setParentId(Const.TREE_ROOT_ID);
            this.setPath("[" + Const.TREE_ROOT_ID + "]");
            this.setLevels(1);
        } else {
            TreeEntity<?> parent = (TreeEntity<?>) this.selectById(this.getParentId());
            this.setParentId(parent.getId());
            this.setPath(parent.getPath() + ",[" + parent.getId() + "]");
            this.setLevels(parent.getLevels()+1);
        }
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Integer getLevels() {
        return levels;
    }

    public void setLevels(Integer levels) {
        this.levels = levels;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public List<T> getChildren() {
        return children;
    }

    public void setChildren(List<T> children) {
        this.children = children;
    }

    public T getParentTree() {
        return parentTree;
    }

    public void setParentTree(T parentTree) {
        this.parentTree = parentTree;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}
